package org.example.lost.controller;

import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.ibatis.io.ResolverUtil;
import org.example.domain.PageInfoDTO;
import org.example.domain.Result;
import org.example.interceptor.HeaderTokenInterceptor;
import org.example.lost.entity.LostInfo;
import org.example.lost.entity.vo.HistoryInfoVO;
import org.example.lost.entity.vo.LostInfoVO;
import org.example.lost.entity.vo.ReturnInfoVO;
import org.example.lost.service.LostService;
import org.example.redis.util.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * 寻物启事子模块的功能：
 * 1、发布寻物启事
 * 2、查询寻物启事信息
 * 3、撤销寻物启事信息
 * 4、更新寻物启事信息
 * 5、查询用户的寻物启事发布历史
 * 6、归还物品
 * @Author hxy
 * @Date 2021/3/15 10:28
 * @Version 1.0
 */
@RestController
@RequestMapping("/core")
@Api(tags = "寻物启事")
//@CrossOrigin(allowCredentials = "true",allowedHeaders="*")
public class LostController {
    @Autowired(required = true)
    LostService lostService;

    @Autowired
    RedisUtils redisUtils;

    @PostMapping(value = "/lost")
    @ApiOperation("发布寻物启事")
    public Result<String> addLost(@RequestBody @Valid LostInfo lostInfo){
        int result = lostService.addLost(lostInfo);
        if(result==1){
            return Result.success("发布寻物启事成功");
        }
        return Result.fail("发布寻物启事失败");
    }

    @PostMapping(value = "/losts")
    @ApiOperation("列表查询寻物启事")
    public Result<PageInfoDTO<LostInfo>> getList(@RequestBody LostInfoVO lostInfoVO){
        PageInfoDTO<LostInfo> list = lostService.getList(lostInfoVO);
        return Result.success(list);
    }

    @DeleteMapping(value = "/lost/{lostId}")
    @ApiOperation("撤销寻物启事")
    public Result delete(@PathVariable(name = "lostId") @ApiParam(name = "lostId",value = "失物id") String lostId){
        int result = lostService.deleteLost(lostId);
        if(result ==1){
            return Result.success("撤销寻物启事成功");
        }
        return Result.fail("撤销寻物启事失败");
    }

    @PutMapping(value = "/lost")
    @ApiOperation("更新寻物启事")
    public Result<String> put(@RequestBody @Valid LostInfo lostInfo){
        int result = lostService.updateLost(lostInfo);
        if(result ==1){
            return Result.success("更新寻物启事成功");
        }
        return Result.fail("更新寻物启事失败");
    }


    @PostMapping(value = "/lostOne")
    @ApiOperation("查询寻物启事发布历史")
    public Result<PageInfoDTO<LostInfo>> getOne(@RequestBody HistoryInfoVO historyInfoVO){
        PageInfoDTO<LostInfo> list = lostService.getOne(historyInfoVO);
        return Result.success(list);
    }


    @PutMapping("/lost/return")
    @ApiOperation("归还物品")
    public Result<String> lost(@RequestBody ReturnInfoVO returnInfoVO,HttpServletRequest request){

        String verifyCode = (String) redisUtils.get(returnInfoVO.getUserPhone());
        if(verifyCode.equals(returnInfoVO.getCode())){
            String userPhone=lostService.returns(returnInfoVO.getUserId(),returnInfoVO.getLostId());
            if(userPhone != null && !userPhone.equals("")){
                return Result.success(userPhone);
            }
            return Result.fail("归还失败");
        }else {
            return Result.fail("验证码错误");
        }


    }

    @PutMapping("/lost/status/{lostId}/{status}")
    @ApiOperation("修改寻物启事的审核状态")
    public Result<String> updateCheckStatus(
            @PathVariable(name = "lostId") @ApiParam(name = "lostId",value = "失物id") String lostId,
            @PathVariable(name = "status") @ApiParam(name = "status",value = "审核状态") String status){
        int result=lostService.updateCheckStatus(lostId,status);
        if(result==1){
            return Result.success("审核状态修改成功");
        }
        return Result.fail("审核状态修改失败");
    }

    @GetMapping("/lost/code/{userPhone}")
    @ApiOperation("给手机号码发送验证码")
    public Result<String> sendCode(HttpServletRequest request,
            @PathVariable(name = "userPhone") @ApiParam(name = "userPhone",value = "手机号码") String userPhone){
        String verifyCode = String.valueOf(new Random().nextInt(899999) + 100000);
        //将验证码存到session中

        redisUtils.set(userPhone,verifyCode,120, TimeUnit.SECONDS);
        return Result.getCode(verifyCode);
    }

}
